package com.example.springboot.swagger2.controller;

import com.example.springboot.swagger2.entity.UserEntity;
import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * </p>
 *
 * @author Zero
 * @since 2019/6/12
 */
@Api(tags = {"用户管理"})
@RestController
@RequestMapping("/api/users")
public class UserController {

    /**
     * 忽略API
     * @param name
     * @return
     */
    @ApiIgnore
    @RequestMapping("/hello")
    public String hello(String name) {
        return "Hello, " + name;
    }

    @ApiOperation(value = "获取所有用户", notes = "获取所有用户信息")
    @ApiResponse(code = 403, message = "禁止访问")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<List<UserEntity>> getAllUsers() {
        UserEntity user1 = new UserEntity();
        user1.setId("1");
        user1.setUsercode("kermit");
        user1.setUsername("克米特");
        UserEntity user2 = new UserEntity();
        user2.setId("2");
        user2.setUsercode("kermit");
        user2.setUsername("克米特");
        List<UserEntity> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.set("1", "test1");
        headers.set("2", "test2");
        headers.set("3", "test3");
        ResponseEntity<List<UserEntity>> res = new ResponseEntity<>(list, headers, HttpStatus.OK);
        return res;
    }

    @ApiOperation(value = "获取用户")
    @ApiImplicitParam(name = "id",value = "用户主键", required = true, dataType = "String", paramType = "path")
    @ApiResponses({
            @ApiResponse(code = 403, message = "禁止访问"),
            @ApiResponse(code = 404, message = "请求路径不存在")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<UserEntity> getUsers(@PathVariable(value = "id") String id) {
        UserEntity user = new UserEntity();
        user.setId(id);
        user.setUsercode("kermit");
        user.setUsername("克米特");
        ResponseEntity<UserEntity> res = new ResponseEntity<>(user, HttpStatus.OK);
        return res;
    }

    @ApiOperation(value = "保存用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "用户编码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "name", value = "用户名称", required = true, dataType = "String")
    })
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String postUsers(String code, String name) {
        return "保存成功：" + code + "/" + name;
    }

    @ApiOperation(value = "更新用户")
    @ApiImplicitParam(name = "id",value = "用户主键", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String putUsers(@PathVariable(value = "id") String id) {
        return "更新成功: " + id;
    }

    @ApiOperation(value = "删除用户")
    @ApiImplicitParam(name = "id",value = "用户主键", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String delUsers(@PathVariable(value = "id") String id) {
        return "删除成功: " + id;
    }

    /**
     * 只会返回headers信息
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.HEAD)
    public String head(String name) {
        return name;
    }

}
